package com.gxa.modules.service.impl;

import com.gxa.common.constant.Constants;
import com.gxa.common.exception.LcException;
import com.gxa.common.utils.GenerateSequenceUtil;
import com.gxa.common.vo.PageRecv;
import com.gxa.common.vo.PageResult;
import com.gxa.common.vo.ResultVo;
import com.gxa.modules.mapper.ContractMapper;
import com.gxa.modules.mapper.GiveLoanMapper;
import com.gxa.modules.pojo.Contract;
import com.gxa.modules.pojo.Loan;
import com.gxa.modules.pojo.LoanSearch;
import com.gxa.modules.pojo.Loan_Processing;
import com.gxa.modules.service.GiveLoanService;
import com.gxa.modules.service.Loanafter_PayBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: microloans_system
 * @description: 放款服务层
 * @author: LC
 * @create: 2021-12-30 17:02
 */

@Service
public class GiveLoanServiceImpl implements GiveLoanService {

    @Autowired(required = false)
    GiveLoanMapper giveLoanMapper;

    @Autowired(required = false)
    ContractMapper contractMapper;

    @Autowired(required = false)
    Loanafter_PayBackService loanafter_payBackService;




    @Override
    public ResultVo modifyLoanChannel(Loan loan) {
        ResultVo r=new ResultVo();
        int result=giveLoanMapper.updateLoanChannel(loan);
        if(result<0){
            throw new LcException(500,"设置放款通道失败！");
        }
        //设置成放款处理状态
        int statusResult=contractMapper.updateLoanState(loan.getLoan_id(),Constants.LOAN_PROCESSING);
        if(statusResult<=0){
            throw new LcException(500,"更新放款处理状态失败！请联系管理员");
        }
        r.setCode(200);
        r.setMsg("更新成功！");
        return r;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo giveLoan(Loan_Processing loan_processing) {
        ResultVo r=new ResultVo();
        int result=giveLoanMapper.insertGiveLoan(loan_processing);
        if(result<=0){
            throw new LcException(500,"系统出现故障！");
        }
        //查找loan
        Loan loan=giveLoanMapper.getLoanById(loan_processing.getLoan_id());

//        添加贷后
        loanafter_payBackService.addPayBack(
                new SimpleDateFormat("yyyy-MM-dd").format(loan_processing.getLdate()),
                new BigDecimal(loan.getCompound_interest()+""),
                new BigDecimal(loan_processing.getAmount()+""),
                loan.getTime_limit(),
                (int)loan_processing.getLoan_id());

        r.setCode(200);
        r.setMsg("放款成功！");
        return r;
    }

    @Override
    public ResultVo entrustLoan(Loan_Processing loan_processing) {
        ResultVo r=new ResultVo();
        //模拟银行返回接口
        loan_processing.setBank_serial_number("No."+ GenerateSequenceUtil.generateSequenceNo());
        loan_processing.setVoucher_url("bank.jpg");
        loan_processing.setLdate(new Date());

        //插入数据库
        int result=giveLoanMapper.insertGiveLoan(loan_processing);
        if(result<=0){
            throw new LcException(500,"系统出现故障！");
        }

        //查找loan
        Loan loan=giveLoanMapper.getLoanById(loan_processing.getLoan_id());

        //        添加贷后
        loanafter_payBackService.addPayBack(
                new SimpleDateFormat("yyyy-MM-dd").format(loan_processing.getLdate()),
                new BigDecimal(loan.getCompound_interest()+""),
                new BigDecimal(loan_processing.getAmount()+""),
                loan.getTime_limit(),
                (int)loan_processing.getLoan_id());


        r.setCode(200);
        r.setMsg("委托放款成功！");
        return r;
    }

    @Override
    public ResultVo getGiveLoanReviewPageInfoByCondition(PageRecv<LoanSearch> loanSearchPageRecv) {
        ResultVo r=new ResultVo();
        //查询放款审批状态的贷款
        List<Integer> states=new ArrayList<>();
        states.add(Constants.LOAN_GIVE_REVIEW);
        loanSearchPageRecv.getData().setLoan_state(states);

        List<Contract> contractList=contractMapper.queryContractPageByCondition(loanSearchPageRecv);
        PageResult pageResult=new PageResult();
        long count=contractMapper.countContractPageByCondition(loanSearchPageRecv);
        pageResult.setTotalSize(count);
        pageResult.setList(contractList);
        r.setCode(200);
        r.setData(pageResult);
        return r;
    }

    @Override
    public ResultVo getLoanProcessingPageInfoByCondition(PageRecv<LoanSearch> loanSearchPageRecv) {
        ResultVo r=new ResultVo();
        //放款处理状态
        List<Integer> states=new ArrayList<>();
        states.add(Constants.LOAN_PROCESSING);
        loanSearchPageRecv.getData().setLoan_state(states);

        List<Contract> contractList=contractMapper.queryContractPageByCondition(loanSearchPageRecv);
        PageResult pageResult=new PageResult();
        long count=contractMapper.countContractPageByCondition(loanSearchPageRecv);
        pageResult.setTotalSize(count);
        pageResult.setList(contractList);
        r.setCode(200);
        r.setData(pageResult);
        return r;
    }
}